{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 书籍名称： Python 神经网络编程\n",
    "### [GitHub地址](https://github.com/makeyourownneuralnetwork/makeyourownneuralnetwork)\n",
    "### [作者博客](https://makeyourownneuralnetwork.blogspot.com/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import scipy.special\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline\n",
    "\n",
    "# neural network class definition\n",
    "class neuralNetwork:\n",
    "    # initialise the neural network\n",
    "    def __init__(self,inputnodes,hiddennodes,outputnodes,learningrate):\n",
    "        self.inodes = inputnodes\n",
    "        self.hnodes = hiddennodes\n",
    "        self.onodes = outputnodes\n",
    "        # learning rate\n",
    "        self.lr = learningrate\n",
    "        # creat the core of neuralnetwork weight matrices\n",
    "        self.wih = np.random.normal(0.0 , pow(self.hnodes , -0.5) , (self.hnodes,self.inodes))\n",
    "        self.who = np.random.normal(0.0 , pow(self.onodes , -0.5) , (self.onodes,self.hnodes))\n",
    "        self.activation_function = lambda x : scipy.special.expit(x)  # 激活函数 S = singmoid(x)\n",
    "        pass\n",
    "    # train the neural network\n",
    "    def train(self,inputs_list,targets_list):\n",
    "        # targets - final_outputs 差就是 预期目标输出值与实际计算值误差\n",
    "        \n",
    "        # convert inputs list to 2d array\n",
    "        inputs = np.array(inputs_list , ndmin = 2 ).T\n",
    "        targets = np.array(targets_list , ndmin = 2).T\n",
    "        \n",
    "        # 1、inputs通过矩阵变换激活函数变成 final_outputs 实际计算误差\n",
    "        # calculate signals into hidden layer\n",
    "        hidden_inputs = np.dot(self.wih , inputs)\n",
    "        # calculate the signals emerging from hidden layer\n",
    "        hidden_outputs = self.activation_function(hidden_inputs)\n",
    "        # calculate signals into final output layer\n",
    "        final_inputs = np.dot(self.who , hidden_outputs)\n",
    "        # calculate the signals emerging from final output layer\n",
    "        final_outputs = self.activation_function(final_inputs)\n",
    "        \n",
    "        # 2、E节点误差 = 目标值与实际计算值得差，改变权重不需要再经过S激活矩阵\n",
    "        # error is the(target - actual)\n",
    "        output_errors = targets - final_outputs\n",
    "        # hidden layer error is the output_errors,split by weights,recombined at hidden nodes ???\n",
    "        hidden_errors = np.dot(self.who.T , output_errors)\n",
    "        \n",
    "        # 3、反向修改权重代码，矩阵公式是数76页完美 np.transpose用于高维矩阵转置\n",
    "        self.who += self.lr * np.dot((output_errors \\\n",
    "                                      * final_outputs * (1.0 - final_outputs)) , np.transpose(hidden_outputs))\n",
    "        self.wih += self.lr * np.dot((hidden_errors \\\n",
    "                                      * hidden_outputs * (1.0 - hidden_outputs)) , np.transpose(inputs))\n",
    "        \n",
    "    # query the neural network\n",
    "    def query(self,inputs_list):\n",
    "        # convert inputs list to 2d array\n",
    "        inputs = np.array(inputs_list , ndmin = 2).T  # 创建2维矩阵T矩阵\n",
    "        \n",
    "        # calculate signals into hidden layer\n",
    "        hidden_inputs =  np.dot(self.wih,inputs)\n",
    "        # calculate the signals emerging from hidden layer\n",
    "        hidden_outputs = self.activation_function(hidden_inputs)\n",
    "        \n",
    "        # calculate signals into final output layer\n",
    "        final_inputs = np.dot(self.who , hidden_outputs)\n",
    "        # calculate the signals emerging from final output layer\n",
    "        final_outputs = self.activation_function(final_inputs)\n",
    "        return final_outputs\n",
    "        # print(inputs)\n",
    "        # print(inputs.shape)\n",
    "        \n",
    "        pass\n",
    "    def printStatus(self):\n",
    "        print(\"此神经网络总共三层，相关信息如下：\")\n",
    "        print(f'输入层inputnodes,有{self.inodes}个节点')\n",
    "        print(f'隐藏层hiddennodes,有{self.hnodes}个节点')\n",
    "        print(f'输出层outputnodes,有{self.onodes}个节点')\n",
    "        print(f'学习率目前是:{self.lr}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.56073811],\n",
       "       [0.35695995],\n",
       "       [0.33290976]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = neuralNetwork(3,3,3,0.3)\n",
    "# n.printStatus()\n",
    "n.query([1.0,0.5,-1.5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable        Type             Data/Info\n",
      "------------------------------------------\n",
      "n               neuralNetwork    <__main__.neuralNetwork object at 0x036A70B0>\n",
      "neuralNetwork   type             <class '__main__.neuralNetwork'>\n",
      "np              module           <module 'numpy' from 'C:\\<...>ges\\\\numpy\\\\__init__.py'>\n",
      "plt             module           <module 'matplotlib.pyplo<...>\\\\matplotlib\\\\pyplot.py'>\n",
      "scipy           module           <module 'scipy' from 'C:\\<...>ges\\\\scipy\\\\__init__.py'>\n"
     ]
    }
   ],
   "source": [
    "%whos"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 开始用MNIST数据集来训练神经网络\n",
    "#### 转换MNIST数据变成csv格式\n",
    "```\n",
    "def convert(imgf, labelf, outf, n):   \n",
    "    f = open(imgf, \"rb\")\n",
    "    o = open(outf, \"w\")\n",
    "    l = open(labelf, \"rb\")\n",
    "    f.read(16)\n",
    "    l.read(8)\n",
    "    images = []\n",
    "    for i in range(n):\n",
    "        image = [ord(l.read(1))]\n",
    "        for j in range(28*28):\n",
    "            image.append(ord(f.read(1)))\n",
    "        images.append(image) \n",
    "    for image in images:\n",
    "        o.write(\",\".join(str(pix) for pix in image)+\"\\n\")\n",
    "    f.close()\n",
    "    o.close()\n",
    "    l.close()\n",
    "convert(\"train-images.idx3-ubyte\", \"train-labels.idx1-ubyte\",\n",
    "        \"mnist_train.csv\", 60000)\n",
    "convert(\"t10k-images.idx3-ubyte\", \"t10k-labels.idx1-ubyte\",\n",
    "        \"mnist_test.csv\", 10000)\n",
    " \n",
    "print(\"Convert Finished!\")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['mnist_test.csv',\n",
       " 'mnist_train.csv',\n",
       " 'Mnist转换csv.txt',\n",
       " 't10k-images.idx3-ubyte',\n",
       " 't10k-labels.idx1-ubyte',\n",
       " 'train-images.idx3-ubyte',\n",
       " 'train-labels.idx1-ubyte']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "path = r\"C:\\Users\\yanglichao\\Desktop\\MNIST数据集\"  # 每个电脑文件路径可能不一样\n",
    "os.chdir(path)\n",
    "os.listdir()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "60000"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_file = open(\"mnist_train.csv\")\n",
    "data_list = data_file.readlines()  # 内存一次读取文件\n",
    "data_file.close()\n",
    "len(data_list) # 看一下data_list有多少个字符串也就是手写数字"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,3,18,18,18,126,136,175,26,166,255,247,127,0,0,0,0,0,0,0,0,0,0,0,0,30,36,94,154,170,253,253,253,253,253,225,172,253,242,195,64,0,0,0,0,0,0,0,0,0,0,0,49,238,253,253,253,253,253,253,253,253,251,93,82,82,56,39,0,0,0,0,0,0,0,0,0,0,0,0,18,219,253,253,253,253,253,198,182,247,241,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,80,156,107,253,253,205,11,0,43,154,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,14,1,154,253,90,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,139,253,190,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,11,190,253,70,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,35,241,225,160,108,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,81,240,253,253,119,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,45,186,253,253,150,27,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,16,93,252,253,187,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,249,253,249,64,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,46,130,183,253,253,207,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,39,148,229,253,253,253,250,182,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,24,114,221,253,253,253,253,201,78,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,23,66,213,253,253,253,253,198,81,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,18,171,219,253,253,253,253,195,80,9,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,55,172,226,253,253,253,253,244,133,11,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,136,253,253,253,212,135,132,16,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0\\n'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_list[0]  # 第一个字符串长度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1846"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(data_list[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0xa830e70>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAADmVJREFUeJzt3X+MVPW5x/HPI4KoEIOyUGLxbtuouYakWx1JDWL2UiXUNAGCNSWxoZF0G63JxRBTs39Yf+QaYi6tGE2T7QXBpLVUAcHEtCgx8ZJodfxVRdSqWcteEJaoVIjSAM/9Yw/NijvfGWbOzBn2eb8SszPnOd89jwMfzsx858zX3F0A4jmt6AYAFIPwA0ERfiAowg8ERfiBoAg/EBThB4Ii/EBQhB8I6vRWHmzy5Mne2dnZykMCofT392v//v1Wy74Nhd/M5klaJWmMpP9x9xWp/Ts7O1Uulxs5JICEUqlU8751P+03szGSHpL0fUmXSFpsZpfU+/sAtFYjr/lnSnrP3T9w939K+oOk+fm0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RdVm6utLK0Cn7s2m3F60I+5JNNZdwvaYykNe7+Xy1vYgRm9k0Nne2loUVMf19kb2b2qKRuDV31tVfSLyU9IemPki6Q9HdJP3T3lr/xVqG3bg09df3Xys3HX2O3uLcrJf2vpDckHcs292ro9XVhj12ir8Uq4HHjE35AUHzCDwiK8ANBEX4gKMIPBEX4gaAIPxAU4QeCIvxAUP8PRZ8Vlgh2BcUAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "all_values = data_list[0].split(\",\")\n",
    "image_array = np.asfarray(all_values[1:]).reshape((28,28))  # 第一个是标记符\n",
    "plt.imshow(image_array , cmap = \"Greys\" , interpolation = \"None\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.02164706 0.07988235 0.07988235 0.07988235\n",
      " 0.49917647 0.538      0.68941176 0.11094118 0.65447059 1.\n",
      " 0.96894118 0.50305882 0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.12647059 0.14976471 0.37494118 0.60788235\n",
      " 0.67       0.99223529 0.99223529 0.99223529 0.99223529 0.99223529\n",
      " 0.88352941 0.67776471 0.99223529 0.94952941 0.76705882 0.25847059\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.20023529\n",
      " 0.934      0.99223529 0.99223529 0.99223529 0.99223529 0.99223529\n",
      " 0.99223529 0.99223529 0.99223529 0.98447059 0.37105882 0.32835294\n",
      " 0.32835294 0.22741176 0.16141176 0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.07988235 0.86023529 0.99223529\n",
      " 0.99223529 0.99223529 0.99223529 0.99223529 0.77870588 0.71658824\n",
      " 0.96894118 0.94564706 0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.32058824 0.61564706 0.42541176 0.99223529\n",
      " 0.99223529 0.80588235 0.05270588 0.01       0.17694118 0.60788235\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.06435294 0.01388235 0.60788235 0.99223529 0.35941176\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.54964706 0.99223529 0.74764706 0.01776471 0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.05270588\n",
      " 0.74764706 0.99223529 0.28176471 0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.14588235 0.94564706\n",
      " 0.88352941 0.63117647 0.42929412 0.01388235 0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.32447059 0.94176471 0.99223529\n",
      " 0.99223529 0.472      0.10705882 0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.18470588 0.73211765 0.99223529 0.99223529\n",
      " 0.59235294 0.11482353 0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.07211765 0.37105882 0.98835294 0.99223529 0.736\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.97670588 0.99223529 0.97670588 0.25847059 0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.18858824 0.51470588 0.72047059 0.99223529\n",
      " 0.99223529 0.81364706 0.01776471 0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.16141176 0.58458824\n",
      " 0.89905882 0.99223529 0.99223529 0.99223529 0.98058824 0.71658824\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.10317647 0.45258824 0.868      0.99223529 0.99223529 0.99223529\n",
      " 0.99223529 0.79035294 0.31282353 0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.09929412 0.26623529 0.83694118 0.99223529\n",
      " 0.99223529 0.99223529 0.99223529 0.77870588 0.32447059 0.01776471\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.07988235 0.67388235\n",
      " 0.86023529 0.99223529 0.99223529 0.99223529 0.99223529 0.76705882\n",
      " 0.32058824 0.04494118 0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.22352941 0.67776471 0.88741176 0.99223529 0.99223529 0.99223529\n",
      " 0.99223529 0.95729412 0.52635294 0.05270588 0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.538      0.99223529\n",
      " 0.99223529 0.99223529 0.83305882 0.53411765 0.52247059 0.07211765\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01       0.01       0.01\n",
      " 0.01       0.01       0.01       0.01      ]\n"
     ]
    }
   ],
   "source": [
    "scaled_input = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01\n",
    "print(scaled_input)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(784,)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "scaled_input.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 主代码 书135页"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 设置 input hidden output nodes\n",
    "input_nodes = 784  # 应为图像是28*28像素\n",
    "hidden_nodes = 100\n",
    "output_nodes =10  # 应为输入的只有[0..9]十个数字\n",
    "\n",
    "# 设置学习率\n",
    "learn_rate = 0.3\n",
    "\n",
    "# 创建神经网络实例\n",
    "n = neuralNetwork(input_nodes,hidden_nodes,output_nodes,learn_rate)\n",
    "\n",
    "# 导入 mnist training data csv\n",
    "training_data_file = open('mnist_train.csv' , 'r')\n",
    "training_data_list = training_data_file.readlines()\n",
    "training_data_file.close()\n",
    "\n",
    "# 从mnist数据集中开始训练\n",
    "for record in training_data_list:\n",
    "    # 数据集list 用 , 分割 参考上面data_list[0]\n",
    "    all_values = record.split(\",\")\n",
    "    # 准备输入列表inputs\n",
    "    inputs = (np.asfarray(all_values[1:]) / 255.0 * 0.99) + 0.01 # 训练组每一行第二个开始才是训练组，第一个是标签label\n",
    "    # 创建目标输出值(all 0.01,except the desired label which is 0.99)\n",
    "    targets = np.zeros(output_nodes) + 0.01\n",
    "    # all_values[0] 复制权重0.99 正确值\n",
    "    targets[int(all_values[0])] = 0.99\n",
    "    n.train(inputs,targets)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Variable             Type             Data/Info\n",
      "-----------------------------------------------\n",
      "all_values           list             n=785\n",
      "data_file            TextIOWrapper    <_io.TextIOWrapper name='<...>ode='r' encoding='cp936'>\n",
      "data_list            list             n=60000\n",
      "hidden_nodes         int              100\n",
      "image_array          ndarray          28x28: 784 elems, type `float64`, 6272 bytes\n",
      "input_nodes          int              784\n",
      "inputs               ndarray          784: 784 elems, type `float64`, 6272 bytes\n",
      "learn_rate           float            0.3\n",
      "n                    neuralNetwork    <__main__.neuralNetwork object at 0x1412A410>\n",
      "neuralNetwork        type             <class '__main__.neuralNetwork'>\n",
      "np                   module           <module 'numpy' from 'C:\\<...>ges\\\\numpy\\\\__init__.py'>\n",
      "os                   module           <module 'os' from 'C:\\\\Us<...>\\\\Anaconda3\\\\lib\\\\os.py'>\n",
      "output_nodes         int              10\n",
      "path                 str              C:\\Users\\yanglichao\\Desktop\\MNIST数据集\n",
      "plt                  module           <module 'matplotlib.pyplo<...>\\\\matplotlib\\\\pyplot.py'>\n",
      "record               str              8,0,0,0,0,0,0,0,0,0,0,0,0<...>0,0,0,0,0,0,0,0,0,0,0,0\\n\n",
      "scaled_input         ndarray          784: 784 elems, type `float64`, 6272 bytes\n",
      "scipy                module           <module 'scipy' from 'C:\\<...>ges\\\\scipy\\\\__init__.py'>\n",
      "targets              ndarray          10: 10 elems, type `float64`, 80 bytes\n",
      "training_data_file   TextIOWrapper    <_io.TextIOWrapper name='<...>ode='r' encoding='cp936'>\n",
      "training_data_list   list             n=60000\n"
     ]
    }
   ],
   "source": [
    "%whos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "测试的是数字2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<matplotlib.image.AxesImage at 0x3b6fb70>"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAP8AAAD8CAYAAAC4nHJkAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAADbpJREFUeJzt3X+M1PWdx/HXGwSj0hAJA12pdnuolxqTAx3JGS6G2kjspRGrwUhMxYAHxBpbbfR0E1P/MTGXaykxtbpFZGuopbGl8od6KqnxMKZhV6VauTuI2VIKsktsUjHKxvV9f+yXZsX9fmeY7/c731nez0dCZub7/v54M/Ca78x8ZuZj7i4A8UypugEA1SD8QFCEHwiK8ANBEX4gKMIPBEX4gaAIPxAU4QeCOq2dB5s9e7Z3d3e385BAKIODgzpy5Ig1s26u8JvZ1ZI2SJoqa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NT/gBQfEJPyAowg8ERfiBoAg/EBThB4Ii/EBQhB8IivADQf0/TWrP8fFj+T8AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# text测试\n",
    "test_data_file = open('mnist_test.csv' , 'r')\n",
    "test_data_list = test_data_file.readlines()\n",
    "test_data_file.close()\n",
    "# 选取测试集第二个\n",
    "all_values = test_data_list[1].split(\",\")\n",
    "print(f\"测试的是数字{all_values[0]}\")\n",
    "image_array = np.asfarray(all_values[1:]).reshape((28,28))  # 第一个是标记符\n",
    "plt.imshow(image_array , cmap = \"Greys\" , interpolation = \"None\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.55145447e-01],\n",
       "       [3.26408869e-02],\n",
       "       [8.60185764e-01],\n",
       "       [6.37458318e-02],\n",
       "       [1.01708722e-04],\n",
       "       [4.61062348e-03],\n",
       "       [6.50556906e-03],\n",
       "       [5.39663702e-04],\n",
       "       [8.06640510e-03],\n",
       "       [2.97337516e-04]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n.query((np.asfarray(all_values[1:]) / 255 *0.99) + 0.01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
